home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 26 / macformat_26.iso / Shareware / Programación / CDEF Set #1 / Extras / Demo App Project Files / AEHandlers.c next >
Text File  |  1996-11-30  |  9KB  |  270 lines

  1. // Apple Event Functions
  2. #ifndef     __FXNPROTOS__
  3. #include "FxnProtos.h"
  4. #endif
  5.  
  6. #include "CDEF Demo App.h"
  7. #include "FxnProtos.h"
  8.  
  9. extern Boolean gDone;
  10.  
  11.  
  12. /***************************************************************************************************
  13.     AEHasRequiredParameters
  14.     
  15.     Checks to see if all the parameters were extracted from the AppleEvent. The keyMissedKeywordAttr
  16.     contains the first required parameter that your handlers didn't retrieve, if any. If doesn't 
  17.     contain the keyMissedKeywordAttr attribute you extracted all the parameters and you can continue 
  18.     to process the AppleEvent. If you not, you should stop processing the AppleEvent.
  19. ***************************************************************************************************/
  20.  
  21. OSErr    AEHasRequiredParameters( AppleEvent *ae )
  22. {
  23.     OSErr            error;
  24.     DescType        returnedType;
  25.     Size            actualSize;
  26.     
  27.     // Look for the missing keyword attribute. If we got it, there is something
  28.     // missing.
  29.     
  30.     error = AEGetAttributePtr( ae, keyMissedKeywordAttr, typeWildCard, &returnedType, 
  31.                                NULL, 0, &actualSize );
  32.     if( error == errAEDescNotFound )
  33.     {
  34.         // You got all the required parameters
  35.     
  36.         return( noErr );
  37.     }
  38.     else
  39.     {
  40.         // You didn't get all the parameters or there was some other error causing
  41.         // a problem. Either way, you need to stop processing the AppleEvent if this
  42.         // function returns anything but, noErr.
  43.         
  44.         if( error != noErr ) return( error );
  45.         return( errAEParamMissed );
  46.     }
  47. }
  48.  
  49.  
  50. /*************************************************************************************
  51.     AEOpenApplication
  52.     
  53.     AppleEvent handler for the OpenApp event. This will be called when the application 
  54.     is launched without any documents to be opened or printed. We just return the 
  55.     error (if any) if we didn't get all the required parameters. You could also have 
  56.     code here to put up a splash screen but, remember that this can be called more
  57.     than just when your application is first launched. The Finder is smart enough
  58.     not to send you another OpenApp event but, other people launching your app thru
  59.     some other means may not be.
  60. *************************************************************************************/
  61.  
  62. pascal    OSErr    AEOpenApplication( AppleEvent *ae, AppleEvent *reply, long refCon )
  63. {
  64.     OSErr        error;
  65.     
  66.     // Check for any missing required parameters
  67.     
  68.     error = AEHasRequiredParameters( ae );
  69.     if( error != noErr ) return( error );
  70.     
  71.     // If we wanted to do anything when the application is launched without any 
  72.     // documents to be opened or printed, we would do it here. Display a splash 
  73.     // screen, open a new window, etc...
  74.     
  75.     // Set the gAutoQuit flag to false since we never want to auto quit when we get 
  76.     // an OpenApplication event (ie. the user double-clicked on us).
  77.     
  78. //    gAutoQuit = false;
  79.  
  80.     return( error );
  81. }
  82.  
  83. /*************************************************************************************
  84.     AEOpenDocument
  85.     
  86.     AppleEvent handler for the OpenDoc event. When the user drops files onto your 
  87.     application or they double click on documents your application created, you'll get 
  88.     this event. We just pass the event onto our generic AEHandleDocuments() with a 
  89.     flag set for opening files.
  90. *************************************************************************************/
  91.  
  92. pascal    OSErr    AEOpenDocuments( AppleEvent *ae, AppleEvent *reply, long refCon )
  93. {
  94.     // This just sets the kOpenDocument flag so we know what type of event it is and 
  95.     // passed all the parameters onto the generic AEHandleDocuments routine.
  96.     
  97.     return( AEHandleDocuments( ae, reply, refCon ) );
  98. }
  99.  
  100. /*************************************************************************************
  101.     AEPrintDocument
  102.     
  103.     AppleEvent handler for the PrintDoc event. When the user prints files from the 
  104.     Finder or some other utility that prints from your application, you'll get this 
  105.     event. We just pass the event onto our generic AEHandleDocuments() routine with 
  106.     a flag for printing files.
  107. *************************************************************************************/
  108.  
  109. pascal    OSErr    AEPrintDocuments( AppleEvent *ae, AppleEvent *reply, long refCon )
  110. {
  111.     // Handle PrintDoc events similar to how OpenDoc events are handle by stepping 
  112.     // thru each document and printing it. Make sure to stay open cause the Finder 
  113.     // will send you a Quit event if you need to quit when done printing.
  114.  
  115. //    gAutoQuit = false;
  116.  
  117.     // If we can interact with the user, we should open all the documents
  118.     // and display the print dialog box to let the user choose the correct
  119.     // settings and print all the documents from with those settings. If
  120.     // we can't interact with the user, we can either print the documents
  121.     // with the default settings or return the errAENoUserInteraction error
  122.     
  123.     return( errAEEventNotHandled );
  124. }
  125.  
  126. /*************************************************************************************
  127.     AEQuitApplication
  128.     
  129.     AppleEvent handler for the QuitApp event. You'll get this when the user chooses 
  130.     quit or when the Finder sends you this event to quit the app for a restart, or 
  131.     some other reason. This should be the only way for the app to quit. We just set a 
  132.     flag indicating we want to quit which force our MainEventLoop to fall thru and 
  133.     quit the application.
  134. *************************************************************************************/
  135.  
  136. pascal    OSErr    AEQuitApplication( AppleEvent *ae, AppleEvent *reply, long refCon )
  137. {
  138.     OSErr            error;
  139.     DescType        saveOptions, returnedType;
  140.     Size            actualSize;
  141.         
  142.     // Check to see if there's a save option parameter in the AppleEvent.
  143.     
  144.     error = AEGetParamPtr( ae, keyAESaveOptions, typeEnumerated, &returnedType, 
  145.                            &saveOptions, sizeof( saveOptions ), &actualSize );
  146.                            
  147.     // If we couldn't find a keyAESaveOptions parameter in this AppleEvent, we
  148.     // assume default (kAEAsk) and set the error to noErr.
  149.                            
  150.     if( error == errAEDescNotFound ) { error = noErr; saveOptions = kAEAsk; }
  151.     if( error != noErr ) return( error );
  152.  
  153.     // Check for any missing required parameters. There is an error in the sample 
  154.     // code in Inside Mac: Interapplication Communication. It says you should call
  155.     // your HasRequiredParameters routine first which you shouldn't since it will
  156.     // incorrectly return that you didn't get all the parameters (since there is
  157.     // possibly the save options parameter which is optional (ie. save "options").
  158.     
  159.     error = AEHasRequiredParameters( ae );
  160.     if( error != noErr ) return( error );
  161.     
  162.     // Set the quit flag so our MainEventLoop will know we want to quit.
  163.  
  164. //    gQuit = true;
  165.  
  166.     gDone = true;
  167.     
  168.     return( error );
  169. }
  170.  
  171. /*************************************************************************************
  172.     AEHandleDocuments
  173.     
  174.     This is our generic AppleEvent handler for dealing with document events. It takes
  175.     the AppleEvent and based on the event type, handles processing the documents
  176.     the correct way.
  177. *************************************************************************************/
  178.  
  179. pascal    OSErr    AEHandleDocuments( AppleEvent *ae, AppleEvent *reply, SInt32 refCon )
  180. {
  181.     OSErr                error;
  182.     AEDescList            list;
  183.     SInt32                count, i;
  184.     FSSpec                fs;
  185.     EventRecord            event;
  186.     
  187.     // Get the direct parameter, a list of descriptors (ie. files in this case), and put it into the 
  188.     // list.
  189.     
  190.     error = AEGetParamDesc( ae, keyDirectObject, typeAEList, &list );
  191.     if( error != noErr ) return( error );
  192.     
  193.     // Check for any missing parameters.
  194.     
  195.     error = AEHasRequiredParameters( ae );
  196.     if( error != noErr ) goto error;
  197.     
  198.     // Count the number of items in the document list (There may be folders which only count as one, 
  199.     // here (they'll be handled correctly later).
  200.     
  201.     error = AECountItems( &list, &count );
  202.     if( error != noErr ) goto error;
  203.     
  204.     for( i = 1; i <= 1; i++ )
  205.     {
  206.         // Coerce the item in the AppleEvent descriptor list into an FSSpec.
  207.         
  208.         error = AEGetNthFSSpec( &list, i, &fs );
  209.         if( error != noErr ) goto error;
  210.         
  211.         // Process the item.
  212.         
  213.         error = ProcessItem( &fs );
  214.         if( error != noErr ) goto error;
  215.     }
  216.  
  217.     // Dispose of the list.
  218.     
  219.     error = AEDisposeDesc( &list );
  220.     return( error );
  221.  
  222. error:
  223.     AEDisposeDesc( &list );
  224.     return( error );
  225. }
  226.  
  227. #pragma mark -
  228.  
  229. /***************************************************************************************************
  230.     ProcessItem
  231. ***************************************************************************************************/
  232.  
  233. OSErr    ProcessItem( FSSpec *inItem )
  234. {
  235.     OSErr        error;
  236.     SInt16        fileRef;
  237.     short        size;
  238.     long        count;
  239.     DialogPtr    dialog;
  240.     short        result;
  241.     Str255        titleTemp;
  242.     MenuHandle    menu;
  243.  
  244.     error = 0;
  245.  
  246.     return( error );
  247. }
  248.  
  249. /***************************************************************************************************
  250.     AEGetNthFSSpec
  251.     
  252.     Pulls out a single FSSpec from an AppleEvent document list.
  253. ***************************************************************************************************/
  254.  
  255. OSErr    AEGetNthFSSpec( AEDescList *documentList, long index, FSSpec *spec )
  256. {
  257.     OSErr                error;
  258.     AEKeyword            keyword;
  259.     DescType            returnedType;
  260.     FSSpec                tempSpec;
  261.     Size                actualSize;
  262.     
  263.     // Get the the Nth pointer in the AppleEvent of type FSSpec.
  264.     
  265.     error = AEGetNthPtr( documentList, index, typeFSS, &keyword, &returnedType,
  266.                          ( Ptr ) &tempSpec, sizeof( tempSpec ), &actualSize );
  267.     *spec = tempSpec;
  268.     return( error );
  269. }
  270.